﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XDesigner.Development.Dom
{
    public class DelphiHelper
    {
        /// <summary>
        /// 加载项目文件
        /// </summary>
        /// <param name="strFileName"></param>
        public static bool LoadSolutionFile( SolutionInfo sln , string strFileName)
        {
            sln.Projects.Clear();
            sln.FileName = strFileName;
            sln.Files.Clear();
            sln.Style = SolutionStyle.Delphi;
            sln.Name = Path.GetFileNameWithoutExtension(strFileName);
            string rootPath = System.IO.Path.GetDirectoryName(strFileName);

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(strFileName);
            foreach (System.Xml.XmlElement element in doc.SelectNodes("BorlandProject/Delphi.Personality/Source/Source"))
            {
                string name = element.InnerText;
                name = System.IO.Path.Combine( rootPath, name);
                if (System.IO.File.Exists(name))
                {
                    ProjectInfo prj = new ProjectInfo();
                    prj.Style = ProjectStyle.Delphi;
                    if (LoadProject(prj, name))
                    {
                        sln.Projects.Add(prj);
                    }
                }
            }//foreach
            return true;
        }

        public static bool LoadProject( ProjectInfo prj , string strFileName)
        {
            string rootPath = Path.GetDirectoryName(strFileName);
            prj.ProjectFileName = strFileName;
            prj.Name = Path.GetFileNameWithoutExtension(strFileName);
            prj.RootPath = Path.GetDirectoryName(strFileName);
            using (StreamReader myReader = new StreamReader(strFileName, System.Text.Encoding.Default, true))
            {
                string strLine = myReader.ReadLine();
                bool ReadingContains = false;
                while (strLine != null)
                {
                    int index = 0;
                    strLine = strLine.Trim();
                    if (strLine.Length == 0 || strLine == "{$R *.res}")
                    {
                        strLine = myReader.ReadLine();
                        continue;
                    }
                    if (strLine == "contains")
                    {
                        ReadingContains = true;
                    }
                    else if (strLine == "end.")
                    {
                        ReadingContains = false;
                    }
                    else if (strLine == "uses")
                    {
                        ReadingContains = true;
                    }
                    if (ReadingContains == false)
                    {
                        if (strLine.StartsWith("{$") && strLine.EndsWith("}"))
                        {
                            strLine = strLine.Substring(2, strLine.Length - 3);
                            index = strLine.IndexOf(' ');
                            if (index > 0)
                            {
                                string name = strLine.Substring(0, index).Trim();
                                strLine = strLine.Substring(index + 1).Trim();
                                if (name == "DESCRIPTION")
                                {
                                    if ( string.IsNullOrEmpty( prj.Name ))
                                    {
                                        prj.Name = RemoveFlag(strLine);
                                    }
                                }
                                else if (name == "R")
                                {
                                    SourceFileInfo rf = new SourceFileInfo();
                                    rf.FileName = RemoveFlag(strLine);
                                    rf.FullFileName = System.IO.Path.Combine(rootPath, rf.FileName);
                                    prj.Files.Add(rf);
                                }
                                else if (name == "I")
                                {
                                    SourceFileInfo ifile = new SourceFileInfo();
                                    ifile.FileName = RemoveFlag(strLine);
                                    ifile.FullFileName = System.IO.Path.Combine(rootPath, ifile.FileName);
                                    prj.Files.Add(ifile);
                                }
                            }
                        }
                    }
                    else
                    {
                        index = strLine.IndexOf(" in ");
                        if (index > 0)
                        {
                            strLine = strLine.Substring(index + 4).Trim();
                            if (strLine.EndsWith(",") || strLine.EndsWith(";"))
                            {
                                strLine = strLine.Substring(0, strLine.Length - 1);
                            }
                            strLine = RemoveFlag(strLine);
                            SourceFileInfo NewFile = new SourceFileInfo();
                            NewFile.FileName = strLine;
                            NewFile.FullFileName = System.IO.Path.Combine(rootPath, strLine);
                            if (strLine.ToLower().EndsWith(".pas"))
                            {
                                NewFile.Style = FileStyle.SourceCode;
                            }
                            prj.Files.Add(NewFile);

                            string name2 = System.IO.Path.ChangeExtension(strLine, "dfm");
                            string name3 = System.IO.Path.Combine(rootPath, name2);
                            if (System.IO.File.Exists(name3))
                            {
                                NewFile = new SourceFileInfo();
                                NewFile.FileName = name2;
                                NewFile.FullFileName = name3;
                                NewFile.Style = FileStyle.Resource;
                                prj.Files.Add(NewFile);
                            }
                        }
                    }//else
                    strLine = myReader.ReadLine();
                }//while
            }//using
            return true;
        }

        private static string RemoveFlag(string txt)
        {
            txt = txt.Trim();
            if (txt.StartsWith("'") )
            {
                int index = txt.IndexOf("'", 1);
                txt = txt.Substring(1, index - 1);
            }
            return txt.Trim();
        }
    }
}
